/***************************************************************************
 *   Copyright (C) 2009 by mj   *
 *   mj_dv@web.de   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

// configure.cpp is a part of the steamserverutil project
// steamserverutil.googlecode.com

#include <QMessageBox>

#include "configure.h"
#include "ui_configure.h"

configure::configure(QWidget *parent) :
    QDialog(parent)
{
    setupUi(this);

    util = new steamserverutil;

    if(!util->is_installed(comboBox))
        QMessageBox::critical(this, tr("Error, no servers found"), tr("Seems like that you have not got any servers installed\nPlease install one"), QMessageBox::Ok | QMessageBox::Default);


    connect(listWidget, SIGNAL(currentRowChanged(int)), stackedWidget, SLOT(setCurrentIndex(int)));
    connect(listWidget, SIGNAL(currentRowChanged(int)), this, SLOT(read()));
    connect(comboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(read()));
    connect(saveButton, SIGNAL(clicked()),this, SLOT(write()));
    connect(copyMapsFromMapFolderButton, SIGNAL(clicked()), this, SLOT(copy_maps_from_mapfolder()));
    connect(newEntryButton, SIGNAL(clicked()), this, SLOT(new_entry()));

    listWidget->setCurrentRow(0);

}

void configure::read()
{

    QApplication::setOverrideCursor(Qt::WaitCursor);

    if(listWidget->currentRow() < 3)
        copyMapsFromMapFolderButton->setVisible(true);
    else
        copyMapsFromMapFolderButton->setVisible(false);


    switch(listWidget->currentRow())
    {
        case 0: //maplist

            {
                int rows = mapListTable->rowCount();


                for(int i = 0; i < rows; i++)
                {
                    mapListTable->removeRow(0);

                }

                util->edit_mapcl(1, comboBox->currentIndex(), 2 /*to read the maplist.txt*/, mapListTable);


                break;
            }

         case 1: //mapcycle
             {
                int rows = mapListTable->rowCount();

                for(int i = 0; i <= rows; i++)
                    mapCycleTable->removeRow(0);

                util->edit_mapcl(2, comboBox->currentIndex(), 2 /*to read the mapcycle.txt*/, mapCycleTable);


                break;
            }

         case 2: //votemaplist
             {
                int rows = voteMapListTable->rowCount();

                for(int i = 0; i <= rows; i++)
                    voteMapListTable->removeRow(0);

                util->edit_mapcl(3, comboBox->currentIndex(), 2 /*to read the votemaplist.txt*/, voteMapListTable);


                break;
            }

          case 3: //server.cfg
              {
                int rows = servercfgTable->rowCount();


                for(int i = 0; i <= rows; i++)
                    servercfgTable->removeRow(0);

                util->servercfg(comboBox->currentIndex(), 2 /*to read server.cfg*/, servercfgTable);


                break;
            }

          case 4: //adminlist.txt
              {
                int rows = adminTable->rowCount();

                for(int i = 0; i <= rows; i++)
                    adminTable->removeRow(0);

                util->mani_admins(comboBox->currentIndex(), 2 /*to read adminlist.txt*/, adminTable);


                break;
            }

          case 5: //wordfilter.txt
              {
                int rows = filterTable->rowCount();

                for(int i = 0; i <= rows; i++)
                    filterTable->removeRow(0);

                util->wordfilter(comboBox->currentIndex(), 2 /*to read wordfilter.txt*/, filterTable);


                break;
            }

          case 6: //rconlist.txt
              {
                int rows = rconTable->rowCount();

                for(int i = 0; i <= rows; i++)
                    rconTable->removeRow(0);

                util->rcon(comboBox->currentIndex(), 2 /*to read rconlist.txt*/, rconTable);


                break;
            }

         case 7: //votequestionlist.txt
              {
                int rows = voteQTable->rowCount();

                for(int i = 0; i <= rows; i++)
                    voteQTable->removeRow(0);

                util->votequestionlist(comboBox->currentIndex(), 2 /*to read votequestionlist.txt*/, voteQTable);


                break;
            }

        case 9: //reserveslots.txt
              {
                int rows = reserveTable->rowCount();

                for(int i = 0; i <= rows; i++)
                    reserveTable->removeRow(0);

                util->ping_reserve(1, comboBox->currentIndex(), 2 /*to read reserveslots.txt*/, reserveTable);


                break;
            }


          case 8: //pingimmunities.txt
              {
                int rows = pingTable->rowCount();

                for(int i = 0; i <= rows; i++)
                    pingTable->removeRow(0);

                util->ping_reserve(2, comboBox->currentIndex(), 2 /*to read pingimmunities.txt*/, pingTable);


                break;
            }

          case 10: //voterconlist.txt
              {
                int rows = voteRconTable->rowCount();

                for(int i = 0; i <= rows; i++)
                    voteRconTable->removeRow(0);

                util->voterconlist(comboBox->currentIndex(), 2 /*to read voterconlist.txt*/, voteRconTable);


                break;
            }

          case 11: //quakesoundlist.txt
              {
                int rows = quakeTable->rowCount();

                for(int i = 0; i <= rows; i++)
                    quakeTable->removeRow(0);

                util->sounds(1, comboBox->currentIndex(), 2 /*to read quakesoundlist.txt*/, quakeTable);


                break;
            }

         case 12: //actionsoundlist.txt
              {
                int rows = actionSoundTable->rowCount();

                for(int i = 0; i <= rows; i++)
                    actionSoundTable->removeRow(0);

                util->sounds(2, comboBox->currentIndex(), 2 /*to read actionsoundlist.txt*/, actionSoundTable);


                break;
            }

         case 13: //soundlist.txt
              {
                int rows = soundTable->rowCount();

                for(int i = 0; i <= rows; i++)
                    soundTable->removeRow(0);

                util->sounds(3, comboBox->currentIndex(), 2 /*to read soundlist.txt*/, soundTable);


                break;
            }
    }

    QApplication::restoreOverrideCursor();

}

void configure::write()
{

    QApplication::setOverrideCursor(Qt::WaitCursor);


    switch(listWidget->currentRow())
    {
        case 0: //maplist


                util->edit_mapcl(1 /*for maplist.txt*/, comboBox->currentIndex(), 1 /*to write the maplist.txt*/, mapListTable);

                break;

         case 1: //mapcycle


                util->edit_mapcl(2 /*for mapcycle.txt*/, comboBox->currentIndex(), 1 /*to write the mapcycle.txt*/, mapCycleTable);


                break;

         case 2: //votemaplist


                util->edit_mapcl(3 /*for votemaplist.txt*/, comboBox->currentIndex(), 1 /*to writethe votemaplist.txt*/, voteMapListTable);


                break;

          case 3: //server.cfg


                util->servercfg(comboBox->currentIndex(), 1 /*to write server.cfg*/, servercfgTable);


                break;

          case 4: //adminlist.txt

                util->mani_admins(comboBox->currentIndex(), 1 /*to write adminlist.txt*/, adminTable);


                break;

          case 5: //wordfilter.txt

                util->wordfilter(comboBox->currentIndex(), 1 /*to write wordfilter.txt*/, filterTable);


                break;

           case 6: //rconlist.txt

                util->rcon(comboBox->currentIndex(), 1 /*to write rconlist.txt*/, rconTable);


                break;

           case 7: //votequestionlist.txt

                util->votequestionlist(comboBox->currentIndex(), 1 /*to write votequestionlist.txt*/, voteQTable);


                break;

          case 9: //reserveslots.txt

                util->ping_reserve(1, comboBox->currentIndex(), 1 /*to write reserveslots.txt*/, reserveTable);


                break;
          case 8: //pingimmunities.txt

                util->ping_reserve(2, comboBox->currentIndex(), 1 /*to write pingimmunities.txt*/, pingTable);


                break;

           case 10: //voterconlist.txt

                util->voterconlist(comboBox->currentIndex(), 1 /*to write voterconlist.txt*/, voteRconTable);


                break;

            case 11: //quakesoundlist.txt

                util->sounds(1, comboBox->currentIndex(), 1 /*to write voterconlist.txt*/, quakeTable);


                break;

           case 12: //actionsoundlist.txt

                util->sounds(2, comboBox->currentIndex(), 1 /*to write actionsoundlist.txt*/, actionSoundTable);


                break;

          case 13: //soundlist.txt

                util->sounds(3, comboBox->currentIndex(), 1 /*to write soundlist.txt*/, soundTable);


                break;
    }

    QApplication::restoreOverrideCursor();
}

void configure::copy_maps_from_mapfolder()
{
    util->edit_mapcl((listWidget->currentRow()+1), comboBox->currentIndex(), 3 /*to copy maps into *.txt */, NULL);

    read(); //to refresh the *Table
}

void configure::new_entry()
{
    switch(listWidget->currentRow())
    {
        case 0: //maplist
                mapListTable->setRowCount(mapListTable->rowCount()+1);

                break;


         case 1: //mapcycle

                mapCycleTable->setRowCount(mapCycleTable->rowCount()+1);

                break;


         case 2: //votemaplist

                voteMapListTable->setRowCount(voteMapListTable->rowCount()+1);


                break;


          case 3: //server.cfg

                servercfgTable->setRowCount(servercfgTable->rowCount()+1);

                break;


          case 4: //adminlist.txt

                adminTable->setRowCount(adminTable->rowCount()+1);

                break;
          case 5: //wordfilter.txt

                filterTable->setRowCount(filterTable->rowCount()+1);

                break;

          case 6: //rconlist.txt

                rconTable->setRowCount(rconTable->rowCount()+1);

                break;

          case 7: //votequestionlist.txt

                voteQTable->setRowCount(voteQTable->rowCount()+1);

                break;


          case 8: //pingimmunities.txt

                pingTable->setRowCount(pingTable->rowCount()+1);

                break;


          case 9: //reserveslots.txt

                reserveTable->setRowCount(reserveTable->rowCount()+1);

                break;
          case 10: //voterconlist.txt

                voteRconTable->setRowCount(voteRconTable->rowCount()+1);

                break;

           case 11: //quakesoundlist.txt

                quakeTable->setRowCount(quakeTable->rowCount()+1);

                break;


           case 12: //actionsoundlist.txt

                actionSoundTable->setRowCount(actionSoundTable->rowCount()+1);

                break;


          case 13: //soundlist.txt

                soundTable->setRowCount(soundTable->rowCount()+1);

                break;


    }
}
